<template>
  <ex-dialog width="800px" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose" append-to-body>
    <div class="main" v-loading="loading">
      <template>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" :label-width="labelWidth" class="demo-ruleForm">

          <el-form-item label="主播提成：" prop="anchorCommission">
            <el-input v-model="ruleForm.anchorCommission" placeholder="请输入" :style="{ width: inputWidth }"><template
                slot="append">%</template></el-input>
          </el-form-item>
          <el-form-item label="中控提成：" prop="controlCommission">
            <el-input v-model="ruleForm.controlCommission" placeholder="请输入" :style="{ width: inputWidth }"><template
                slot="append">%</template></el-input>
          </el-form-item>
        </el-form>
      </template>
    </div>
    <div class="button_box" v-if="pathType != 'watch'">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="AgreeLoading" @click="submitForm('ruleForm')">保存提交</el-button>
    </div>
  </ex-dialog>
</template>
<script>
import Loading from "@/components/Loading/index";
// api
import {
  getCommission, commissionSave
} from "@/api/customer/serviceData";
export default {
  components: {
    Loading,
  },
  props: {
    visible: Boolean,
    id: String,
    pathType: String,
  },
  data() {
    var checkNum = (rule, value, callback) => {
      let reg = /(^[0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/; //大于等于0的数值且最多保留两位小数的数值
      if (value === '') {
        callback(new Error('请输入'));
      } else if (!reg.test(value)) {
        callback(new Error('请输入大于等于0的数值'));
      } else if (value > 100) {
        callback(new Error('请输入小于等于100的数值'));
      } else {
        callback();
      }
    };
    return {
      labelWidth: "140px",
      inputWidth: "430px",
      title: "佣金配置",
      loading: false,
      disabled: false,
      AgreeLoading: false,
      ruleForm: {
        anchorCommission:'',
        controlCommission:'',
      },
      rules: {
        anchorCommission: [
          { required: true, validator: checkNum, trigger: ["change", "bulr"] }
        ],
        controlCommission: [
          { required: true, validator: checkNum, trigger: ["change", "bulr"] }
        ],
      },
    };
  },
  computed: {},
  created() {
    console.log(this.pathType)
    this.getCommission()
  },
  mounted() {

  },
  computed: {},
  methods: {
    //获取详情
    getCommission() {
      getCommission(this.id).then(res => {
        this.ruleForm = res.result
      })
    },
    //取消
    handleClose(formName) {
      this.$emit("update:visible", false);
    },
    submitForm(formName) {
      //创建确定
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.AgreeLoading = true
          var data = {
            ...this.ruleForm,
            id:this.id
          }
          commissionSave(data).then(res => {
            this.handleClose()
            this.$message.success('操作成功')
            this.$emit('success')
            this.AgreeLoading = false
          }).catch(() => {
            this.AgreeLoading = false
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
    padding: 0 !important;
  }

  ::v-deep .ex--dialog--main {
    padding: 10px 20px !important;
  }
}

.el-select,
.el-input,
.el-cascader {
  width: 400px;
}

.main {
  padding: 20px 20px;
}

.button_box {
  display: flex;
  justify-content: center;
}

.el-radio-group {
  display: flex;
  flex-direction: column;

  .el-radio {
    margin-bottom: 14px;
  }
}

.form_item {
  display: flex;
}

.form_item_label {
  margin-left: 18px;
  color: #606266;
  font-size: 14px;
  font-weight: bold;
}

.form_item_detail {
  margin-left: 10px;
}
</style>